var tigerStep2 = {
	load  : function () {
		this.eventRegister();
		waitShow();
		Ext.Ajax.request({
			url : "/ad/sys/com/control/AdCategorService.php?action=getAll",
			callback : function (options, se, resp) {
			   var tpl = new Ext.XTemplate(
		            '<tpl for=".">',
		                '<label><input name="category[]" type="checkbox"  value="{Value}"/>{Text}</label>{[xindex % 4==0 ? "<br>" : ""]}',
		            '</tpl>' 
		        );
		        tpl.overwrite("categoryGroup", resp.responseText.toJSON());
		        tigerStep2.setFormValue();
		        waitHide();
			}
		}); 
		
	    
		
	    $('#add').click(function() {  
	       return !$('#tempArea option:selected').remove().appendTo('#area_');  
	    });
	    
	    $('#remove').click(function() {  
	       return !$('#area_ option:selected').remove().appendTo('#tempArea');  
	    });  
	   
	},
	
	setFormValue : function () {
		if (ad != null) {
			if (ad.area) {
				var areas = ad.area.split(",");
				for (var i=0;i<areas.length;i++) {
					$('#tempArea option[value='+areas[i]+']').remove().appendTo('#area_')
				}
				Ext.getDom("area").value = ad.area;
			}
			if (ad.ageStart) {
				Ext.getDom("ageStart").value = ad.ageStart;
			}
				
			if (ad.ageStart) {
				Ext.getDom("ageEnd").value = ad.ageEnd;
			}
			if (ad.sex) {
				document.getElementsByName("sex")[ad.sex].checked = true;
			}	 
			
			if (ad.category) {
				var checks = document.getElementsByName("category[]");
				
			    for (var i=0; i < ad.category.length ;i++) {
			    	checks[ad.category[i]-1].checked = true;	
			    }
			}
		}
	},
	
	eventRegister : function () {
		Ext.get("btnPrevious").on("click", this.previous);
		Ext.get("btnNext").on("click", this.next);
	},
	
	previous : function () {
		window.location ="/ad/html/frontoffice/tigerStep1.php";
	},
	
	next : function () {
		 var area = "";
		 var areaText = "";
		 var op = Ext.getDom("area_");
		  
		 for (var i=0;i < op.length; i++) {
		 	area+=op[i].value+ ","; 
		 	areaText += op[i].text + ",";
		 }
		 
		 if (op.length > 1) {
		 	area = area.substr(0,area.length-1);
		 	areaText = areaText.substr(0, areaText.length-1);
		 }
		 
		 
		 
		 if (area == "" ) {
		 	alert("請選擇[國家]!");
		 	Ext.getDom("area").focus();
		 	return;
		 }
		 
		 if (Ext.getDom("ageStart").value == "") {
		 	alert("請輸入[年齡]!");
		 	Ext.getDom("ageStart").focus();
		 	return ;
		 }
		
		 
		 if (isNaN(Ext.getDom("ageStart").value)) {
		 	alert("[年齡]請輸入數字!");
		 	Ext.getDom("ageStart").focus();
		 	return ;
		 }
		 
		   if (Ext.getDom("ageEnd").value == "") {
		 	alert("請輸入[年齡]!");
		 	Ext.getDom("ageEnd").focus();
		 	return ;
		 }
		 
		 
		  if (isNaN(Ext.getDom("ageEnd").value)) {
		 	alert("[年齡]請輸入數字!");
		 	Ext.getDom("ageEnd").focus();
		 	return ;
		 }
		 
		 if (Ext.getDom("ageStart").value > Ext.getDom("ageEnd").value) {
		 	alert("[年齡]範圍輸入錯誤!");
		 	return;
		 }
		 
		 var category = document.getElementsByName("category[]");
		 var isChk = false ;
		 for (var i=0; i < category.length;i++) {
		 	if (category[i].checked) {
		 		isChk = true;
		 		break;
		 	}
		 }
		 if (!isChk) {
		 	alert("[刊登類別] 最少選擇一項!");
		 	return;
		 }
	     
	 	 
		 Ext.getDom("areaName").value = areaText;
		 Ext.getDom("area").value = area;
		 Ext.getDom("form").submit();
		  
	}
};

window.onload = function () {
	tigerStep2.load();	
}